﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace AbyssCombatLogger
{

    // Create a node sorter that implements the IComparer interface.
    public class NodeSorterByTag : IComparer
    {
        public NodeSorterByTag()
        {
        }
        // Compare the length of the strings, or the strings
        // themselves, if they are the same length.
        public int Compare(object x, object y)
        {
            TreeNode tx = x as TreeNode;
            TreeNode ty = y as TreeNode;

            TreeNode xParent = tx.Parent;
            TreeNode yParent = ty.Parent;
            if (xParent == null && yParent ==null)
            {
                ////x and y probably an inner node where the key is the player and the text is the threat
                //if (ty.Text.StartsWith("Total"))
                //{
                //    return -1; // since xx < yy where xx and yy are threat values
                //}
                //else if (tx.Text.StartsWith("Total"))
                //{
                //    return 1; //since xx > yy
                //}
                //else
                    return string.Compare(tx.Text, ty.Text); //sort by name
            }
            else
            {
                bool useTagComparison = true;

                /*if (tx.IsSelected)
                {
                    return 1;
                }
                else if (ty.IsSelected)
                    return -1;
                */
                
                if (tx.Checked && !ty.Checked)
                {
                    //put checked on top
                    return -1;
                }
                else if (ty.Checked && !tx.Checked)
                {
                    //put checked on top
                    return 1;
                }
                else if (!tx.Checked && !ty.Checked || (tx.Checked && ty.Checked))
                {
                    useTagComparison = true;
                }

                if (useTagComparison)
                {
                    frmMeter.TagForMeterNodes xTag = (frmMeter.TagForMeterNodes)tx.Tag;
                    frmMeter.TagForMeterNodes yTag = (frmMeter.TagForMeterNodes)ty.Tag;

                    float XThreat;
                    if (xTag != null)
                        XThreat = Convert.ToSingle(xTag.Amount); //use the tag to hold the threat value
                    else
                        XThreat = 0;
                    float YThreat;
                    if (yTag != null)
                        YThreat = Convert.ToSingle(yTag.Amount);//use the tag to hold the threat value
                    else
                        YThreat = 0;

                    int compResult = YThreat.CompareTo(XThreat); //make the checked appear on top, 
                    return compResult;
                }
                else { return 0; } //for compiler error which will never happen right now: not all paths return a value
            }

        }
    }
}
